package com.ssm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.ssm.base.BaseController;
import com.ssm.entity.Admin;
import com.ssm.service.AdminService;
import com.ssm.service.RoleService;
import com.ssm.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @Author jie_ge
 * @Date 2022/4/15
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    private static final String SUCCESS_PAGE="common/successPage";
    @Reference
    private AdminService adminService;
    @Reference
    private RoleService roleService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 跳转到index页面
     * @param model
     * @param request
     * @return
     */
    @RequestMapping
    public String index(ModelMap model, HttpServletRequest request){
        //获取请求参数
        Map<String, Object> filters = getFilters(request);
        //调用roleService中分页及待条件查询的方法
        PageInfo<Admin> page = adminService.findPage(filters);
        model.put("page",page);
        model.put("filters",filters);
        return "admin/index";
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @GetMapping("/create")
    public String create(){
        return "admin/create";
    }

    /**
     * 保存admin信息
     * @param admin
     * @return
     */
    @PostMapping("/save")
    public String save(Admin admin,HttpServletRequest request){
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        admin.setHeadUrl("http://47.93.148.192:8080/group1/M00/03/F0/rBHu8mHqbpSAU0jVAAAgiJmKg0o148.jpg");
        adminService.insert(admin);
        return SUCCESS_PAGE;
    }

    /**
     * 根据id删除信息
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        adminService.deleteById(id);
        return "redirect:/admin";
    }

    /**
     * 跳转到修改页面  并根据id获取信息
     * @return
     */
    @GetMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable Long id,ModelMap model){
        Admin adminById = adminService.getById(id);
        model.put("admin",adminById);
        return "admin/update";
    }

    /**
     * 修改
     * @param admin
     * @return
     */
    @PostMapping("update")
    public String update(Admin admin){
        adminService.update(admin);
        return SUCCESS_PAGE;
    }

    /**
     * 上传图片
     * @param map
     * @param id
     * @return
     */
    @GetMapping("/uploadShow/{id}")
    public String uploadShow(Map map,@PathVariable("id") Long id) {
        map.put("id",id);
        return "admin/upload";
    }

    /**
     * 保存上传图片
     * @param id
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("/upload/{id}")
    public String upload(@PathVariable("id") Long id, @RequestParam("file")MultipartFile file,HttpServletRequest request) throws IOException {
        String newFileName = UUID.randomUUID().toString();
        //上传图片
        QiniuUtils.upload2Qiniu(file.getBytes(),newFileName);
        String url = "http://ral7nlzq1.hn-bkt.clouddn.com/"+newFileName;
        Admin admin = new Admin();
        admin.setId(id);
        admin.setHeadUrl(url);
        adminService.update(admin);
        return "common/successPage";
    }

    /**
     * 进入分配角色页面
     * @param map
     * @param adminId
     * @return
     */
    @GetMapping("/assignShow/{adminId}")
    public String assignShow(Map map,@PathVariable("adminId") Long adminId){
        Map<String, Object> roleMap = roleService.findRoleByAdminId(adminId);
        map.putAll(roleMap);
        map.put("adminId",adminId);
        return "admin/assignShow";
    }
    @PostMapping("/assignRole")
    public String assignRole(Long adminId,Long[] roleIds){
        roleService.saveUserRoleRealtionShip(adminId,roleIds);
        return "common/successPage";
    }
}
